//
//  THomeTableView.m
//  LoveShare
//
//  Created by ways on 2017/4/20.
//  Copyright © 2017年 tikeyc. All rights reserved.
//

#import "THomeTableView.h"

#import "TFindDriverListCell.h"
#import "TPassengerListCell.h"
#import "THomeHelperListCell.h"
#import "TShoppingListCell.h"
#import "TSellThingsListCell.h"
#import "THealthLifeListCell.h"

#import "JYJEssenceViewController.h"

#import "THomeTableViewCell.h"

static NSString *homeTableCellIndextifier = @"homeTableCell";

@interface THomeTableView ()<UITableViewDataSource,UITableViewDelegate> {
    BOOL _isNotFirstComing;
    
    NSMutableArray *_types;
}

@end

@implementation THomeTableView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    _types = [NSMutableArray array];
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 6; j++) {
            if (j == 2 || j == 5) {
                //第一版本去掉该两种类型
            } else {
                [_types addObject:@(j)];
            }
            
        }
    }
    
    [self initProperty];
}


#pragma mark - init

- (void)initProperty {
    self.dataSource = self;
    self.delegate = self;
    
    self.tableFooterView = [UIView  new];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _types.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSNumber *typeNum = _types[indexPath.row];
    NSUInteger type = [typeNum integerValue];
    switch (type) {
        case THomeTableCellTypeFindDriver:
        {
            return 270;
        }
            break;
        case THomeTableCellTypeFindPassenger:
        {
            return 180;
        }
            break;
        case THomeTableCellTypeHomeHelper:
        {
            return 200;
        }
            break;
        case THomeTableCellTypeShopping:
        {
            return 340;
        }
            break;
        case THomeTableCellTypeSellThings:
        {
            return 100;
        }
            break;
        case THomeTableCellTypeHealthyLifeStyle:
        {
            return 300;
        }
            break;
            
        default:
            break;
    }
    
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSNumber *typeNum = _types[indexPath.row];
    NSUInteger type = [typeNum integerValue];
    switch (type) {
        case THomeTableCellTypeFindDriver:
        {
            TFindDriverListCell *cell = [tableView dequeueReusableCellWithIdentifier:findDriverListCellindextifier];
            if (cell == nil) {
                cell = [TFindDriverListCell loadFromNib];
            }
            
            return cell;
        }
            break;
    case THomeTableCellTypeFindPassenger:
        {
            TPassengerListCell *cell = [tableView dequeueReusableCellWithIdentifier:findPassengerListCellindextifier];
            if (cell == nil) {
                cell = [TPassengerListCell loadFromNib];
            }
            
            return cell;
        }
        break;
    case THomeTableCellTypeHomeHelper:
        {
            THomeHelperListCell *cell = [tableView dequeueReusableCellWithIdentifier:homeHelperListCellindextifier];
            if (cell == nil) { 
                cell = [THomeHelperListCell loadFromNib];
            }
            
            return cell;
        }
        break;
    case THomeTableCellTypeShopping:
        {
            TShoppingListCell *cell = [tableView dequeueReusableCellWithIdentifier:shoppingListCellindextifier];
            if (cell == nil) {
                cell = [TShoppingListCell loadFromNib];
            }
            
            return cell;
        }
        break;
    case THomeTableCellTypeSellThings:
        {
            TSellThingsListCell *cell = [tableView dequeueReusableCellWithIdentifier:sellThingsListCellindextifier];
            if (cell == nil) {
                cell = [TSellThingsListCell loadFromNib];
            }
            
            return cell;
        }
        break;
    case THomeTableCellTypeHealthyLifeStyle:
        {
            THealthLifeListCell *cell = [tableView dequeueReusableCellWithIdentifier:healthLifeListCellindextifier];
            if (cell == nil) {
                cell = [THealthLifeListCell loadFromNib];
            }
            
            return cell;
        }
        break;
        
        default:
            break;
    }
    
    return nil;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!_isNotFirstComing) {
        cell.left = TScreenWidth;
        NSTimeInterval duration = 0.5;
        [UIView animateWithDuration:duration
                              delay:(indexPath.row * duration) * 0.1
             usingSpringWithDamping:0.5
              initialSpringVelocity:1.0
                            options:UIViewAnimationOptionCurveLinear
                         animations:^{
                             cell.left = 0;
                         }
                         completion:^(BOOL finished) {
                             
                         }];

    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    JYJEssenceViewController *carVC = [[JYJEssenceViewController alloc] init];
    [self.viewController.navigationController pushViewController:carVC animated:YES];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    _isNotFirstComing = YES;
}

@end





